package com.bosssoft.trainee.nontax.service.impl;

import com.bosssoft.trainee.nontax.dao.RoleDao;
import com.bosssoft.trainee.nontax.dao.UserDao;
import com.bosssoft.trainee.nontax.service.UserService;
import com.bosssoft.trainee.nontax.entity.dto.UserDto;
import com.bosssoft.trainee.nontax.entity.dto.UserInfo;
import com.bosssoft.trainee.nontax.entity.po.User;
import com.bosssoft.trainee.nontax.entity.vo.UserVo;
import com.bosssoft.trainee.nontax.util.mapper.dtoVo.UserDtoVo;
import com.bosssoft.trainee.nontax.util.mapper.poDto.UserPoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2022-06-05 21:26:20
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    @Resource
    private RoleDao roleDao;

    @Autowired
    private UserPoDto userPoDto;

    @Autowired
    private UserDtoVo userDtoVo;

    /**
     * @Description: 通过UserDTO 获取用户信息 userinfo
     * @param user user dto
     * @return: com.bosssoft.trainee.nontax.entity.dto.UserInfo
     * @Author: Luo WenLong
     * @Date: 2022/6/24 21:19
     */
    @Override
    public UserInfo getUserInfoFromUser(UserDto user) {
        List<String> roles = new ArrayList<>();
        roleDao.getRolesByUserId(user.getUserId()).forEach(
                role -> roles.add(role.getRoleName()));

        return new UserInfo(
                roles,
                user.getDescription(),
                "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
                user.getName());
    }

    /**
     * @Description: 获取该角色授予的所有用户
     * @param roleId role id
     * @return: java.util.List<com.bosssoft.trainee.nontax.entity.vo.UserVo>
     * @Author: Luo WenLong
     * @Date: 2022/6/24 21:19
     */
    @Override
    public List<UserVo> queryAllByRoleId(Integer roleId) {
        return userDtoVo.dto2vo(userPoDto.po2dto(userDao.queryAllByRoleId(roleId)));
    }

    /**
     * @Description: 通过ID查询单条数据
     * @param userId user id
     * @return: com.bosssoft.trainee.nontax.entity.vo.UserVo
     * @Author: Luo WenLong
     * @Date: 2022/6/24 21:20
     */
    @Override
    public UserVo queryById(Integer userId) {
        return userDtoVo.dto2vo(userPoDto.po2dto(this.userDao.queryById(userId)));
    }

    /**
     * @Description: 查询符合条件的所有用户
     * @param user user dto
     * @return: java.util.List<com.bosssoft.trainee.nontax.entity.vo.UserVo>
     * @Author: Luo WenLong
     * @Date: 2022/6/24 21:21
     */
    @Override
    public List<UserVo> queryByPage(UserDto user) {
        return userDtoVo.dto2vo(userPoDto.po2dto(this.userDao.queryAllByLimit(user)));
    }

    /**
     * @Description: 新增数据
     * @param user user dto
     * @return: com.bosssoft.trainee.nontax.entity.vo.UserVo
     * @Author: Luo WenLong
     * @Date: 2022/6/24 21:21
     */
    @Override
    public UserVo insert(UserDto user) {
        this.userDao.insert(user);
        return userDtoVo.dto2vo(user);
    }

    /**
     * @Description: 修改数据
     * @param user user dto
     * @return: com.bosssoft.trainee.nontax.entity.vo.UserVo
     * @Author: Luo WenLong
     * @Date: 2022/6/24 21:21
     */
    @Override
    public UserVo update(UserDto user) {
        this.userDao.update(user);
        return this.queryById(user.getUserId());
    }

    /**
     * @Description: 通过主键删除数据
     * @param userId 用户id
     * @return: boolean
     * @Author: Luo WenLong
     * @Date: 2022/6/24 21:22
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.userDao.deleteById(userId) > 0;
    }

    /*
     * @Description: 能否删除
     * @param user id
     * @return: java.lang.String
     * @Author: Luo WenLong
     * @Date: 2022/6/26 15:19
     */
    @Override
    public String canDeleteUser(Integer roleId) {
        if(roleId == 1001 || roleId == 1002 || roleId == 1003 ){
            return "can't delete this role, no authorization";
        } else {
            return null;
        }
    }

    /**
     * @Description: 对用户进行软删除
     * @param userId 用户id
     * @return: int -1表示该用户不能删除
     * @Author: Luo WenLong
     * @Date: 2022/6/24 17:55
     */
    @Override
    public int deleteByIdSoft(Integer userId) {
        if(canDeleteUser(userId) != null){
            return 0;
        }
        User user = userDao.queryById(userId);
        return user.getIsValid() < 0 ? -1 : userDao.deleteByIdSoft(userId);
    }
}
